package artid.com.arttogoa.adapter;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import artid.com.arttogoa.R;
import artid.com.arttogoa.parser.RecordList;
import artid.com.arttogoa.volley.BitmapCache;
import com.android.volley.RequestQueue;
import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.NetworkImageView;
import com.android.volley.toolbox.Volley;

public class ArtRecordAdapter extends BaseAdapter {
	private Context mContext;
	LayoutInflater mInflater;
	ViewHolder holder;
	private ImageLoader mImageLoader;
	List<RecordList> listdata;
	RequestQueue mQueue;
	String width;

	public ArtRecordAdapter(Context mContext, String width) {
		this.mContext = mContext;
		this.width = width;
		mInflater = LayoutInflater.from(mContext);
		listdata = new ArrayList<RecordList>();
		mQueue = Volley.newRequestQueue(mContext);
		mImageLoader = new ImageLoader(mQueue, new BitmapCache());
	}

	public void setdata(List<RecordList> listdata) {
		this.listdata = listdata;
		notifyDataSetChanged();
	}

	@Override
	public int getCount() {
		return listdata.size();
	}

	@Override
	public Object getItem(int position) {
		return null;
	}

	@Override
	public long getItemId(int position) {
		return 0;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		ViewHolder vh = null;
		if (convertView == null) {
			vh = new ViewHolder();
			convertView = mInflater.inflate(R.layout.item_art_record, null);
			vh.iv = (NetworkImageView) convertView
					.findViewById(R.id.iv_art_img);
			vh.com = (ImageView) convertView.findViewById(R.id.iv_complete);
			vh.text = (TextView) convertView.findViewById(R.id.tv_status);
			vh.tittle = (TextView) convertView.findViewById(R.id.tv_aname);
			convertView.setTag(vh);
		} else {
			vh = (ViewHolder) convertView.getTag();
		}
		RecordList item = listdata.get(position);
		if (item.getImgurl() != null && !item.getImgurl().equals("")) {
			vh.iv.setDefaultImageResId(R.drawable.yspba_img_mor);
			vh.iv.setErrorImageResId(R.drawable.yspba_img_mor);
			vh.iv.setImageUrl(item.getImgurl() + "_"
					+ (Integer.parseInt(width) / 2) + "x10.png", mImageLoader);
		}
		Drawable drawable = null;
		if (item.getIs_phone().equals("2")) {
			drawable = mContext.getResources().getDrawable(
					R.drawable.a_ico_iphone);
		} else if (item.getIs_phone().equals("1")) {
			drawable = mContext.getResources().getDrawable(
					R.drawable.a_ico_shebei);
		}
		// / 这一步必须要做,否则不会显示.
		drawable.setBounds(0, 0, drawable.getMinimumWidth(),
				drawable.getMinimumHeight());
		vh.tittle.setCompoundDrawables(drawable, null, null, null);
		vh.tittle.setCompoundDrawablePadding(20);
		vh.tittle.setText(item.getTitle());
		String retype = item.getRegisteredstate();
		if (retype.equals("-1")) {
			vh.com.setVisibility(View.GONE);
			vh.text.setVisibility(View.VISIBLE);
			vh.text.setText("未通过审核");
		} else if (retype.equals("2")) {
			vh.com.setVisibility(View.VISIBLE);
			vh.text.setVisibility(View.GONE);
		} else if (retype.equals("1")) {
			vh.com.setVisibility(View.GONE);
			vh.text.setVisibility(View.VISIBLE);
			vh.text.setText("审核中");
		}

		return convertView;
	}

	class ViewHolder {
		RelativeLayout _rel;
		TextView text, tittle;
		ImageView com;
		NetworkImageView iv;
	}
}
